﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JenkinsCodeBuid.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Serilog;

namespace JenkinsCodeBuid.Controllers
{
    public class UploadFilesController : Controller
    {
        private static IConfiguration _configuration;

        public UploadFilesController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> GetAllSite()
        {
            try
            {
                var websitesUrl = _configuration.GetSection("OnLineSetting:WebSitesUrl").Value;

                var data = await RestClientHelper.PostData<List<string>>(websitesUrl, new { key = "" });

                var list = new List<string>();
                return Ok(data);
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }

        StringBuilder sb = new StringBuilder();
        [HttpPost]
        public async Task<string> UpLoadFiles([FromBody] List<string> webSites)
        {
            try
            {
                CommonConst.Msg = new System.Text.StringBuilder();
                sb = new StringBuilder();
                CommonConst.Count = 0;
                CommonConst.Msg.AppendLine("开始上传");
                CommonConst.UploadSite = _configuration.GetSection("OnLineSetting:UploadSite").Value;
                if (string.IsNullOrWhiteSpace(CommonConst.UploadSite))
                    return "没找到上传文件加路径";
                if (!webSites.Any())
                {
                    throw new Exception("请选择一个站点");
                }
                var baseUrl = _configuration.GetSection("OnLineSetting:BaseUrl").Value;
                //var tasks = new List<Task>();
                foreach (var webSite in webSites)
                {
                    Log.Error($"{webSite}开始");
                    var site = webSite.Split(':')[0];
                    CommonConst.Website = webSite;
                    await BuildCodeHelper.UploadReleaseSite(site, _configuration, baseUrl);

                    CommonConst.Msg.AppendLine("上传成功");
                    var str = $@"{CommonConst.Website}已上传{CommonConst.Count}个文件---------------
                       {CommonConst.Msg.ToString()}%{baseUrl}";
                    sb.Insert(0, str);
                    //tasks.Add(siteTask);
                }
                //await Task.WhenAll(tasks);
                return sb.ToString();
            }
            catch (Exception e)
            {
                CommonConst.Msg.AppendLine(e.Message);
                throw e;
                //return e.Message;
            }
        }
        [HttpPost]
        public string GetMsg()
        {
            return $@"{sb}***************{CommonConst.Website}已上传{CommonConst.Count}个文件--------
                       {CommonConst.Msg.ToString()}";
            //+ "/r/rn错误" + CommonConst.ErrorMsg.ToString();
        }
    }
}
